<!-- 搜索表单 -->
<template>
  <ele-card :body-style="{ paddingBottom: '2px' }">
    <el-form
      ref="formRef"
      inline=""
      :model="form"
      label-width="80px"
      @keyup.enter="search"
    >
      <el-form-item label="会员姓名" prop="realname">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.realname"
          placeholder="用户姓名"
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="tel">
        <el-input clearable :maxlength="20" v-model="form.tel" />
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-select
          clearable
          v-model="form.gender"
          placeholder="不限性别"
          class="ele-fluid"
        >
          <el-option value="" label="不限" />
          <el-option :value="1" label="男" />
          <el-option :value="0" label="女" />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="">
        <el-button type="primary" :loading="loading" @click="search">
          查找所有用户
        </el-button>
        <el-button type="success" :loading="loading" @click="search">
          查找企业人才库
        </el-button>
        <el-button type="" :loading="loading" @click="createApply">
          添加并报名
        </el-button>
      </el-form-item>
    </el-form>
  </ele-card>
</template>

<script setup>
  import { ref } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { createAndApply } from '@/api/company';
  const emit = defineEmits(['search']);

  const props = defineProps({
    /** 修改回显的数据 */
    jobId: Number
  });

  /** 表单数据 */
  const [form, resetFields] = useFormData({
    realname: '',
    tel: '',

    gender: ''
  });

  const loading = ref(false);

  /** 搜索 */
  const search = () => {
    emit('search', { ...form });
  };

  /**  重置 */
  const reset = () => {
    resetFields();
    search();
  };

  // 添加并报名
  const createApply = () => {
    if (form.realname === '' && form.tel === '') {
      EleMessage.warning('请填写姓名和手机!');
      return false;
    }

    if (!/^1[23456789]\d{9}$/.test(form.tel)) {
      EleMessage.warning('请填写正确的手机格式!');
      console.log(form.tel);
      return false;
    }

    if (form.gender === '') {
      EleMessage.warning('请选择性别!');
      return false;
    }

    ElMessageBox.confirm(
      '添加' + form.realname + '，电话' + form.tel + '并报名此工作！',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        const id = props.jobId;
        createAndApply({
          job_id: id,
          realname: form.realname,
          tel: form.tel,
          gender: form.gender
        })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
</script>
